package com.office.web.controller.system;

import com.office.common.annotation.Log;
import com.office.common.core.controller.BaseController;
import com.office.common.core.domain.AjaxResult;
import com.office.common.core.page.TableDataInfo;
import com.office.common.enums.BusinessType;
import com.office.common.utils.SecurityUtils;
import com.office.common.utils.poi.ExcelUtil;
import com.office.system.domain.KnowledgeBases;
import com.office.system.service.IKnowledgeBasesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 知识库管理Controller
 *
 * @author 数字牧马人
 */
@RestController
@RequestMapping("/kowledge/bases")
@Tag(name = "知识库管理")
public class KnowledgeBasesController extends BaseController
{
    @Autowired
    private IKnowledgeBasesService knowledgeBasesService;

    /**
     * 查询知识库管理列表
     */
    @PreAuthorize("@ss.hasPermi('kowledge:bases:list')")
    @GetMapping("/list")
    @Operation(summary = "获取知识库列表")
    public TableDataInfo list(KnowledgeBases knowledgeBases)
    {
        startPage();
        List<KnowledgeBases> list = knowledgeBasesService.selectKnowledgeBasesList(knowledgeBases);
        return getDataTable(list);
    }

    /**
     * 导出知识库管理列表
     */
    @PreAuthorize("@ss.hasPermi('kowledge:bases:export')")
    @Log(title = "知识库管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @Operation(summary = "导出知识库列表")
    public void export(HttpServletResponse response, KnowledgeBases knowledgeBases)
    {
        List<KnowledgeBases> list = knowledgeBasesService.selectKnowledgeBasesList(knowledgeBases);
        ExcelUtil<KnowledgeBases> util = new ExcelUtil<KnowledgeBases>(KnowledgeBases.class);
        util.exportExcel(response, list, "知识库管理数据");
    }

    /**
     * 获取知识库管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('kowledge:bases:query')")
    @GetMapping(value = "/{id}")
    @Operation(summary = "根据id获取知识库详细信息")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(knowledgeBasesService.selectKnowledgeBasesById(id));
    }

    /**
     * 新增知识库管理
     */
    @PreAuthorize("@ss.hasPermi('kowledge:bases:add')")
    @Log(title = "知识库管理", businessType = BusinessType.INSERT)
    @PostMapping
    @Operation(summary = "新增知识库")
    public AjaxResult add(@RequestBody KnowledgeBases knowledgeBases)
    {
        return toAjax(knowledgeBasesService.insertKnowledgeBases(knowledgeBases));
    }

    /**
     * 修改知识库管理
     */
    @PreAuthorize("@ss.hasPermi('kowledge:bases:edit')")
    @Log(title = "知识库管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @Operation(summary = "修改知识库")
    public AjaxResult edit(@RequestBody KnowledgeBases knowledgeBases)
    {
        return toAjax(knowledgeBasesService.updateKnowledgeBases(knowledgeBases));
    }

    /**
     * 删除知识库管理
     */
    @PreAuthorize("@ss.hasPermi('kowledge:bases:remove')")
    @Log(title = "知识库管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @Operation(summary = "根据id删除知识库")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        // 检查是否为管理员
        if (!SecurityUtils.hasRole("admin")) {
            return AjaxResult.error("只有管理员才能删除知识库");
        }
        return toAjax(knowledgeBasesService.deleteKnowledgeBasesByIds(ids));
    }
}
